package com.tarena.baking.controller;


import com.tarena.baking.common.enums.ContentTypeEnum;
import com.tarena.baking.common.ex.ServiceException;
import com.tarena.baking.mapper.ContentMapper;
import com.tarena.baking.pojo.param.ContentParam;
import com.tarena.baking.pojo.entity.Content;
import com.tarena.baking.pojo.vo.ContentIndexVO;
import com.tarena.baking.pojo.vo.ContentSimpleVO;
import com.tarena.baking.response.JsonResult;
import com.tarena.baking.response.StatusCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;


@Validated
@RestController
@RequestMapping("/api/content")
public class ContentController {

    @Autowired
    private ContentMapper contentMapper;

    @GetMapping("getContentListByType")
    public List<ContentIndexVO> getContentListByType(Long type, Long categoryId) {
        if (!ContentTypeEnum.isValidEnum(type)) {
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND,"类型找不到");
        }
        return contentMapper.getContentListByType(type, categoryId);
    }

    @GetMapping("getContentListForManagementByType")
    public List<ContentIndexVO> getContentListForManagementByType(Long userId, Long type, Long categoryId) {
        return contentMapper.getContentListForManagementByType(userId, type, categoryId);
    }

    @GetMapping("getContentListForAdminByType")
    public List<ContentIndexVO> getContentListForAdminByType(Long type, Long categoryId) {
        return contentMapper.getContentListForAdminByType(type, categoryId);
    }

    @PostMapping("saveContent")
    public void saveContent(@Valid @RequestBody ContentParam contentParam) {
        if (contentParam.getId() == null) {
            //更新或者插入
            Content content = new Content();
            BeanUtils.copyProperties(contentParam, content);
            content.setCreateTime(new Date())
                    .setViewCount(0)
                    .setCommentCount(0);
            contentMapper.insert(content);
        } else {
            //更新或者插入
            Content content = new Content();
            BeanUtils.copyProperties(contentParam, content);
            content.setUpdateTime(new Date());
            contentMapper.update(content);
        }
    }

    @RequestMapping("deleteById")
    public void deleteById(int id) {
        contentMapper.deleteById(id);
    }

    @RequestMapping("getContentForEdit")
    public JsonResult getContentForEdit(Long id) {
        return JsonResult.ok(contentMapper.getForEditById(id));
    }

    @RequestMapping("getContentDetail")
    public JsonResult getContentDetail(Long id) {
        contentMapper.addViewCount(id);
        return JsonResult.ok(contentMapper.getDetailById(id));
    }

    @RequestMapping("getUserOtherContent")
    public List<ContentSimpleVO> getUserOtherContent(Long id, Long userId) {
        System.out.println("id = " + id + ", userId = " + userId);
        return contentMapper.getUserOtherContent(id, userId);
    }

    @RequestMapping("getHotContent")
    public List<ContentSimpleVO> getHotContent() {
        return contentMapper.getHotContent();
    }

    @GetMapping("search")
    public List<ContentIndexVO> search(String wd) {
        System.out.println("wd = " + wd);
        return contentMapper.getContentListByWd(wd);
    }
}
